package world

import (
	"7qule.com/sd3/CrossServer/src/common/onlinelog"
	"7qule.com/sd3/CrossServer/src/utils"
	"fmt"
	"time"

	. "7qule.com/sd3/CrossServer/src/model/commonModel"
	"public.com/Framework/goroutineMgr"
)

var (
	oneMinuteHandlerMap  = make(map[string]func(time.Time) error, 4)
	fiveMinuteHandlerMap = make(map[string]func(time.Time) error, 4)
	halfHourHandlerMap   = make(map[string]func(time.Time) error, 4)
	hourHandlerMap       = make(map[string]func(time.Time) error, 4)
	zeroHandlerMap       = make(map[string]func(time.Time) error, 4)
)

func startTimer() {
	// 处理goroutine数量
	goroutineName := fmt.Sprintf("%s.timer", serverModuleName)
	goroutineMgr.Monitor(goroutineName)
	defer goroutineMgr.ReleaseMonitor(goroutineName)

	for {
		now := time.Now()

		// 每分钟的方法
		handleOneMinuteTimer(now)

		// 每５分钟的方法
		if now.Minute()%SystemConstant_FiveMinute == SystemConstant_Zero {
			handleFiveMinuteTimer(now)
		}

		// 每半小时的方法
		if now.Minute()%SystemConstant_ThirtyMinute == SystemConstant_Zero {
			handleHalfHourTimer(now)
		}

		// 每小时的方法
		if now.Minute()%SystemConstant_SixtyMinute == SystemConstant_Zero {
			handleHourTimer(now)
		}

		// 0点的方法
		if now.Hour() == SystemConstant_Zero && now.Minute() == SystemConstant_Zero {
			handleZeroTimer(now)
		}

		// 休眠1分钟
		sleepSecond := 60 - now.Second()
		time.Sleep(time.Second * time.Duration(sleepSecond))
	}
}

func handleOneMinuteTimer(now time.Time) {
	for name, handler := range oneMinuteHandlerMap {
		go func(tempName string, tempHandler func(time.Time) error) {
			// 处理goroutine数量
			goroutineName := fmt.Sprintf("%s.handleOneMinuteTimer", serverModuleName)
			goroutineMgr.MonitorZero(goroutineName)
			defer goroutineMgr.ReleaseMonitor(goroutineName)
			defer utils.LogErrorRecover()
			err := tempHandler(now)
			if err != nil {
				onlinelog.ErrorLog("One minute function failed. Name:%s, Error:%v", tempName, err)
			}
		}(name, handler)
	}
}

func handleFiveMinuteTimer(now time.Time) {
	for name, handler := range fiveMinuteHandlerMap {
		go func(tempName string, tempHandler func(time.Time) error) {
			// 处理goroutine数量
			goroutineName := fmt.Sprintf("%s.handleFiveMinuteTimer", serverModuleName)
			goroutineMgr.MonitorZero(goroutineName)
			defer goroutineMgr.ReleaseMonitor(goroutineName)
			defer utils.LogErrorRecover()
			err := tempHandler(now)
			if err != nil {
				onlinelog.ErrorLog("Five minute function failed. Name:%s, Error:%v", tempName, err)
			}
		}(name, handler)
	}
}

func handleHalfHourTimer(now time.Time) {
	for name, handler := range halfHourHandlerMap {
		go func(tempName string, tempHandler func(time.Time) error) {
			// 处理goroutine数量
			goroutineName := fmt.Sprintf("%s.handleHalfHourTimer", serverModuleName)
			goroutineMgr.MonitorZero(goroutineName)
			defer goroutineMgr.ReleaseMonitor(goroutineName)
			defer utils.LogErrorRecover()
			err := tempHandler(now)
			if err != nil {
				onlinelog.ErrorLog("Half hour function failed. Name:%s, Error:%v", tempName, err)
			}
		}(name, handler)
	}
}

func handleHourTimer(now time.Time) {
	for name, handler := range hourHandlerMap {
		go func(tempName string, tempHandler func(time.Time) error) {
			// 处理goroutine数量
			goroutineName := fmt.Sprintf("%s.handleHourTimer", serverModuleName)
			goroutineMgr.MonitorZero(goroutineName)
			defer goroutineMgr.ReleaseMonitor(goroutineName)
			defer utils.LogErrorRecover()
			err := tempHandler(now)
			if err != nil {
				onlinelog.ErrorLog("Hour function failed. Name:%s, Error:%v", tempName, err)
			}
		}(name, handler)
	}
}

func handleZeroTimer(now time.Time) {
	for name, handler := range zeroHandlerMap {
		go func(tempName string, tempHandler func(time.Time) error) {
			// 处理goroutine数量
			goroutineName := fmt.Sprintf("%s.handleZeroTimer", serverModuleName)
			goroutineMgr.MonitorZero(goroutineName)
			defer goroutineMgr.ReleaseMonitor(goroutineName)
			defer utils.LogErrorRecover()
			err := tempHandler(now)
			if err != nil {
				onlinelog.ErrorLog("Zero function failed. Name:%s, Error:%v", tempName, err)
			}
		}(name, handler)
	}
}

func RegisterOneMinuteHandler(name string, handler func(time.Time) error) {
	if _, exists := oneMinuteHandlerMap[name]; exists {
		panic(fmt.Sprintf("One minute handler of name:%s is already existed.", name))
	}
	oneMinuteHandlerMap[name] = handler
}

func RegisterFiveMinuteHandler(name string, handler func(time.Time) error) {
	if _, exists := fiveMinuteHandlerMap[name]; exists {
		panic(fmt.Sprintf("Five minute handler of name:%s is already existed.", name))
	}
	fiveMinuteHandlerMap[name] = handler
}

func RegisterHalfHourHandler(name string, handler func(time.Time) error) {
	if _, exists := halfHourHandlerMap[name]; exists {
		panic(fmt.Sprintf("Half hour handler of name:%s is already existed.", name))
	}
	halfHourHandlerMap[name] = handler
}

func RegisterHourHandler(name string, handler func(time.Time) error) {
	if _, exists := hourHandlerMap[name]; exists {
		panic(fmt.Sprintf("Hour handler of name:%s is already existed.", name))
	}
	hourHandlerMap[name] = handler
}

func RegisterZeroHandler(name string, handler func(time.Time) error) {
	if _, exists := zeroHandlerMap[name]; exists {
		panic(fmt.Sprintf("Zero handler of name:%s is already existed.", name))
	}
	zeroHandlerMap[name] = handler
}
